/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package data;

import java.sql.*;
import utils.Tabelas;
import utils.Transaction;
import java.util.LinkedList;

/**
 *
 * @author Tiago
 */
public class AskerData {

    public void incluir(AskerDO asker, Transaction tr) throws Exception {
        Connection con = tr.getConnection();
        String sql = "insert into " + Tabelas.ASKER + "(nome_empresa,"
                + "CNPJ,ddd,tel,endereco,nome_contato,sobrenome_contato,"
                + "email_contato,senha) values (?,?,?,?,?,?,?,?,?)";
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setString(1, asker.getNome_empresa());
        ps.setLong(2, asker.getCNPJ());
        ps.setInt(3, asker.getDdd());
        ps.setInt(4, asker.getTel());
        ps.setString(5, asker.getEndereco());
        ps.setString(6, asker.getNome_contato());
        ps.setString(7, asker.getSobrenome_contato());
        ps.setString(8, asker.getEmail_contato());
        ps.setString(9, asker.getSenha());
        int result = ps.executeUpdate();

    }

    public void editar(AskerDO asker,Transaction tr) throws Exception {
        Connection con = tr.getConnection();
        String sql = "update " + Tabelas.ASKER + " set nome_empresa = ?, "
                + "CNPJ=?, ddd=? ,tel=? ,endereco=?, nome_contato=? ,"
                + "sobrenome_contato=?, email_contato =?, senha=? where id=?;";
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setString(1, asker.getNome_empresa());
        ps.setLong(2, asker.getCNPJ());
        ps.setInt(3, asker.getDdd());
        ps.setInt(4, asker.getTel());
        ps.setString(5, asker.getEndereco());
        ps.setString(6, asker.getNome_contato());
        ps.setString(7, asker.getSobrenome_contato());
        ps.setString(8, asker.getEmail_contato());
        ps.setString(9, asker.getSenha());
        ps.setInt(10, asker.getId());
        int result = ps.executeUpdate();

    }

    public AskerDO buscar(String email_cadastro, String senha_cadastro, Transaction tr) throws Exception {
        Connection con = tr.getConnection();
        String sql = "select * from " + Tabelas.ASKER + " where (email_contato=? "
                + "and senha=?)";
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setString(1, email_cadastro);
        ps.setString(2, senha_cadastro);
        ResultSet rs = ps.executeQuery();
        rs.next();
        AskerDO contato = new AskerDO();
        contato.setId (rs.getInt("id"));
        contato.setNome_empresa(rs.getString("nome_empresa"));
        contato.setCNPJ(rs.getLong("CNPJ"));
        contato.setDdd(rs.getInt("ddd"));
        contato.setTel(rs.getInt("tel"));
        contato.setEndereco(rs.getString("endereco"));
        contato.setNome_contato(rs.getString("nome_contato"));
        contato.setSobrenome_contato(rs.getString("sobrenome_contato"));
        contato.setEmail_contato(rs.getString("email_contato"));
        contato.setSenha(rs.getString("senha"));

        return contato;
    }
    
    public boolean confereEmail(String email, Transaction tr) throws Exception{
        Connection con  = tr.getConnection();
        String sql = "select * from " +Tabelas.ASKER+ " where email_contato = ?";
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setString(1, email);
        ResultSet rs = ps.executeQuery();
        rs.last();
        if (rs.getRow()==0){
            return true;
        } else {
            return false;
        }
        
        
    }
    
   //Busca pontos do user e retorna uma lista de Data object dos pontos
    public static LinkedList<PesquisaDO> buscarPesquisas(int askerID, Transaction tr) throws Exception 
    {
        Connection con = tr.getConnection();
        String sql = "select * from " + Tabelas.PESQUISA + " where (asker_id=?)";
        String s = String.valueOf(askerID);
        
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setString(1, s);
        ResultSet rs = ps.executeQuery();
        LinkedList<PesquisaDO> pArray = new LinkedList<PesquisaDO>();
         while (rs.next()) 
         {
            PesquisaDO p = new PesquisaDO();
            p.setAsker_id(rs.getInt("asker_id"));
            p.setNome_Pesquisa(rs.getString("nome_pesquisa"));
            p.setPesquisa_Paga(rs.getBoolean("pesquisa_paga"));
            pArray.add(p);
        }
        return pArray;
    }
    
}
